import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from "../views/HomeView.vue"

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    children: [
    //   {
    //     path: '/users',
    //     name: 'users',
    //     component: () => import(/* webpackChunkName: "about" */ '../views/users.vue')
    //   },
    //   {
    //     path: '/roles',
    //     name: 'roles',
    //     component: () => import(/* webpackChunkName: "about" */ '../views/roles.vue')
    //   },
    //   {
    //     path: '/rights',
    //     name: 'rights',
    //     component: () => import(/* webpackChunkName: "about" */ '../views/rights.vue')
    //   },
    //   {
    //     path: '/categories',
    //     name: 'categories',
    //     component: () => import(/* webpackChunkName: "about" */ '../views/categories.vue')
    //   },
    //   {
    //     path: '/params',
    //     name: 'params',
    //     component: () => import(/* webpackChunkName: "about" */ '../views/params.vue')
    //   },
    //   {
    //     path: '/goods',
    //     name: 'goods',
    //     component: () => import(/* webpackChunkName: "about" */ '../views/goods.vue')
    //   },
    //   {
    //     path: '/add',
    //     name: 'add',
    //     component: () => import(/* webpackChunkName: "about" */ '../views/add.vue')
    //   },  
    //   {
    //     path: '/orders',
    //     name: 'orders',
    //     component: () => import(/* webpackChunkName: "about" */ '../views/orders.vue')
    //   },  
    //   {
    //     path: '/reports',
    //     name: 'reports',
    //     component: () => import(/* webpackChunkName: "about" */ '../views/reports.vue')
    //   },  
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue')
  },
  {
    path: '*',
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue')
  },
  {
    path: '/pardialog',
    name: 'pardialog',
    component: () => import(/* webpackChunkName: "about" */ '../views/pardialog.vue')
  },


]

const router = new VueRouter({
  routes
})

import { fn } from "../utils/index"
function wyy() {
  let token = localStorage.getItem("token")
  let menus = JSON.parse(localStorage.getItem("menus"))
  if (token && menus) {
    let menu = fn(menus)
    menu.forEach(item => {
      router.addRoute('home', item)
    })
  }
}
wyy()

router.beforeEach((to, from, next) => {
  if (localStorage.getItem("token")) {
    next()
  } else {
    if (to.path == "/login") {
      next()
    } else {
      next("/login")
    }
  }
})

export default router
